package com.sanjiahantang.youjiaclient.module.money.views.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.youjiaclient.module.money.presenters.MoneyPresenter;
import com.sanjiahantang.youjiaclient.module.money.views.MoneyView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.module.common.views.SelectorView;
import com.sanjiahantang.youjiaclient.module.money.beans.BankCardBean;

import java.util.List;

/**
 * 添加银行卡
 * Created by admin on 2017/10/28.
 */

public class MeAddBankListActivity extends BaseMvpActivity<MoneyView, MoneyPresenter> implements MoneyView, View.OnClickListener {
    private View fl_Left;
    private TextView title, btn_besure;
    private MyListView myListView;
    private QuickAdapter<BankCardBean.Item> adapter;
    private BankCardBean.Item currentItem = null;
    private String initIndex;
    private boolean canSelect = false;

    @Override
    public MoneyPresenter initPresenter() {
        return new MoneyPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_add_banklist);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);
        btn_besure = (TextView) findViewById(R.id.btn_beSure);
        myListView = (MyListView) findViewById(R.id.bank_list);
        canSelect = getIntent().getBooleanExtra(KEY_TYPE,false);

        adapter = new QuickAdapter<BankCardBean.Item>($this, R.layout.item_me_banklist) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final BankCardBean.Item item) {

                helper.setText(R.id.tvBankName, item.getBankGenneralName());
                helper.setText(R.id.tvBankNo, String.format("尾号%s",Utils.getLast4(item.getBankCardNo())));
                if (TextUtils.equals("1", item.getIsDefault())) {
                    helper.setChecked(R.id.cbSelected, true);
                } else {
                    helper.setChecked(R.id.cbSelected, false);
                }

                CheckBox cbSelected = helper.getView(R.id.cbSelected);
                if (!cbSelected.isChecked()){
                    helper.setOnClickListener(R.id.cbSelected, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            presenter.updateDefaultBankCard($this, item.getMbciId(), new OnLoadListener() {
                                @Override
                                public void onLoad(Object content) {
                                    onResume();
                                }
                            });
                        }
                    });
                }else{
                    helper.setOnClickListener(R.id.cbSelected, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onResume();
                        }
                    });
                }
                helper.setImageResource(R.id.ivBank, R.drawable.i_bank_default);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (canSelect){
                            currentItem = item;
                            Intent intent = new Intent();
                            intent.putExtra(KEY_PARAM, currentItem);
                            setResult(RESULT_OK, intent);
                            finish();
                        }
                    }
                });
            }
        };
        myListView.setAdapter(adapter);

    }

    @Override
    public void initData() {
        title.setText("我的银行卡");


        initIndex = getIntent().getStringExtra(SelectorView.KEY_INDEX);
        btn_besure.setOnClickListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        loadList();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.btn_beSure:
                startActivity(new Intent(this, MeAddBankActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        super.onActivityResult(requestCode, resultCode, data);
    }

    private void loadList(){
        presenter.checkBankListInfo($this, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                List<BankCardBean.Item> param = (List<BankCardBean.Item>) content;
                adapter.clear();
                adapter.addAll(param);
                adapter.notifyDataSetChanged();
            }
        });
    }

    public static void fromSettings(Context context){
        Intent intent = new Intent(context,MeAddBankListActivity.class);
        intent.putExtra(KEY_TYPE,false);
        context.startActivity(intent);
    }
}
